<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :	31 March 2010
// version :	31 March 2010

/**
 * Base exception for all exception inside this library
 *
 * The constant's code value should be converted in string with a crc32 (eg. MY_CONST = crc32('MY_CONST'))
 */
class Cmis_Exception extends Exception
{
    const FOLLOW_UP = 120783507;
    protected $_previous;

    /**
     * @param string|Exception $message if is an Exception, other parameters will be ignored
     * @see Exception
     */
    public function __construct($message = '', $code = 0, Exception $previous = null)
    {
        if (is_a($message, 'Exception'))
        {
            parent::__construct($message->getMessage(), self::FOLLOW_UP, $message);
        }
        parent::__construct($message, $code);
        $this->_previous = $previous;//, $previous);
    }

    /**
     * Contains an exception that need to get the previous or not?
     * @return bool
     */
    public function isFollowUp()
    {
        return ($this->getCode() == self::FOLLOW_UP);
    }

    /**
     * Get the previous exception
     * @return Exception
     */
    public function getPrevious()
    {
        return $this->_previous;
    }

    public function __toString()
    {
        return parent::__toString() . ($this->isFollowUp()?("\r\n" . (string)$this->getPrevious()): ''); 
    }
}

# EOF
